import UIKit

class ViewController: UIViewController {
    
    // Lazy Initializers
    lazy var addBtn: UIButton = {
        let button = UIButton(frame: CGRect(x: 40, y: 100, width: 44, height: 44))
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        button.setTitle("＋", for: .normal)
        button.setTitleColor(.black, for: .normal)
        button.addTarget(self, action: #selector(addImageView(_:)), for: .touchUpInside)
        button.layer.cornerRadius = 22
        button.layer.masksToBounds = true
        button.layer.borderColor = UIColor.black.cgColor
        button.layer.borderWidth = 1.0
        return button
    }()
    
    lazy var tapGesture: UITapGestureRecognizer = {
        let gesture = UITapGestureRecognizer(target: self, action: #selector(tapGestureAction(_:)))
        return gesture
    }()
    // Properties
   
    
    var addImage: UIImage = UIImage(named: "demoImg.jpg")!
    var addViewList: [HYHAddImageView4] = []
    // Constants
    let Ctrl_Radius: CGFloat = 20.0
    
    // View Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        createSubview()
        addImageView(addBtn)
    }
    
    // Methods
    func createSubview() {
        view.backgroundColor = .white
        view.addSubview(addBtn)
        view.addGestureRecognizer(tapGesture)
    }
    
    // Event Handlers
    @objc func addImageView(_ btn: UIButton) {
        cleanSubviewEditStatus()
        let viewSize = getAddImageViewSize()
        let frame = CGRect(x: (view.bounds.size.width - viewSize.width) / 2.0,
                           y: (view.bounds.size.height - viewSize.height) / 2.0,
                           width: viewSize.width,
                           height: viewSize.height)
        
        let imageView = HYHAddImageView4(frame: frame)
        view.addSubview(imageView)
        addViewList.insert(imageView, at: 0)
        imageView.addImage = addImage
        imageView.isEditing = true
        
        imageView.SetEditBlock = { [weak self] in
            self?.cleanSubviewEditStatus()
        }
        
        imageView.RemoveViewBlock = { [weak self] in
            if let index = self?.addViewList.firstIndex(where: { $0.isEditing }) {
                self?.addViewList.remove(at: index)
            }
        }
    }
    
    // Private Methods
    func getAddImageViewSize() -> CGSize {
        
        var width: CGFloat = 0
        var height: CGFloat = 0
        let defaultWH: CGFloat = 100
        
        if addImage.size.width > addImage.size.height {
            height = defaultWH
            width = (height * addImage.size.width / addImage.size.height)
        } else {
            width = defaultWH
            height = (width * addImage.size.height / addImage.size.width)
        }
        
        width += Ctrl_Radius * 2
        height += Ctrl_Radius * 2
        
        return CGSize(width: width, height: height)
    }
    
    func cleanSubviewEditStatus() {
        addViewList.forEach { $0.isEditing = false }
    }
    
    // Gesture Recognizer Action
    @objc func tapGestureAction(_ tapGesture: UITapGestureRecognizer) {
        cleanSubviewEditStatus()
    }
  
}
